package com.songcan.oauth.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songcan.oauth.service.SysUserService;
import com.songcan.oauth.vo.UserVO;
import lombok.AllArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;


@AllArgsConstructor
@RestController
@RequestMapping("rest/user")
@Validated
public class UserController {


    private final SysUserService sysUserService;




    /**
     * 列表
     * @param keyWord
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public IPage<UserVO> list(String keyWord,
                              @RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "20")Integer pageSize){
        return sysUserService.selectByUserPage(new Page(page,pageSize),keyWord);
    }


    /**
     * 新增用户
     * @param userName
     * @param password
     * @param name
     * @param deptId
     * @param phone
     * @param sex
     * @param avatar
     * @param roleId
     * @param remark
     * @param birthDay
     * @param locked
     * @param enabled
     */
    @PostMapping()
    public void  addUser(@NotBlank(message = "用户名不能为空")String userName,
                         @NotBlank(message = "密码不能为空")String password,String name,
                         Long deptId, String phone, Integer sex, String avatar, Long roleId,
                         String remark, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date birthDay,
                         @RequestParam(defaultValue = "0") Integer locked,Long positionId,
                         @RequestParam(defaultValue = "1") Integer enabled){
        sysUserService.addUser(userName,password,deptId,phone,sex,avatar,roleId,remark,birthDay,name,locked,enabled,positionId);
    }

    /**
     * 修改用户
     * @param id
     * @param userName
     * @param password
     * @param name
     * @param deptId
     * @param phone
     * @param sex
     * @param avatar
     * @param roleId
     * @param remark
     * @param birthDay
     * @param locked
     * @param enabled
     */
    @PutMapping
    public void updateUser(@NotNull(message = "id不能为空") Long id, String userName, String password,String name,
                           Long deptId, String phone, Integer sex, String avatar, Long roleId,
                           String remark, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date birthDay,
                           @RequestParam(defaultValue = "0") Integer locked,Long positionId,
                           @RequestParam(defaultValue = "1") Integer enabled){
        sysUserService.updateUser(id,userName,password,deptId,phone,sex,avatar,roleId,remark,birthDay,name,locked,enabled,positionId);
    }

    @GetMapping()
    public UserVO getByModel(@NotNull(message = "id不能为空")Long id){
         return sysUserService.selectByDetail(id);
    }


    /**
     * 删除用户
     * @param id
     */
    @DeleteMapping()
    public void deleteByUser(@NotNull(message = "id不能为空")Long id){
         sysUserService.deleteByUser(id);
    }




}
